package dev.loong.boot3.controller;

import dev.loong.boot3.pojo.User;
import dev.loong.boot3.service.UserService;
import dev.loong.boot3.vo.ResultVO;
import dev.loong.boot3.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {
    private final UserService userService;
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
    @GetMapping("/{id}")
    public User getUser(@PathVariable("id") Long id) {
        return userService.getUser(id);
    }
    @PostMapping("/insertUser")
    public User insertUser(String userName, String note) {
        var user = new User();
        user.setUserName(userName);
        user.setNote(note);
        return userService.insertUser(user);
    }
    @GetMapping(path = {"", "/"})
    public List<User> findUsers(String userName, String note) {
        return userService.findUsers(userName, note);
    }
    @RequestMapping("/updateUserName")
    public Map<String, Object> updateUserName(Long id, String userName) {
        var user = userService.updateUserName(id, userName);
        boolean flag = user != null;
        var message = flag ? "更新成功" : "更新失败";
        return resultMap(flag, message);
    }
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteUser(@PathVariable("id") Long id) {
        var flag = userService.deleteUser(id) > 0;
        var message = flag ? "删除成功" : "删除失败";
        return resultMap(flag, message);
    }

    @GetMapping("/hello/{id}")
    public ModelAndView details(@PathVariable("id") Long id) {
        var mav = new ModelAndView("user/hello");
        var user = userService.getUser(id);
        mav.addObject("user", user);
        return mav;
    }
    @GetMapping("/details/json/{id}")
    public ModelAndView detailsForJson(@PathVariable("id") Long id) {
        var user = userService.getUser(id);
        var mv = new ModelAndView("user/details");
        var jsonView = new MappingJackson2JsonView();
        mv.setView(jsonView);
        mv.addObject("user", user);
        return mv;
    }

    @GetMapping("/list")
    public ModelAndView allUsers() {
        var userList = userService.findUsers(null, null);
        var mv = new ModelAndView("user/list");
        mv.addObject("users", userList);
        return mv;
    }

    @GetMapping("/index")
    public String index() {
        return "user/index";
    }

    @PostMapping("/insert")
    public ResultVO insert(@RequestBody UserVo userVo) {
        var user = UserVo.changeToPo(userVo);
        var result = userService.insert(user);
        if (result == 0) {
            return new ResultVO<UserVo>(false, "插入失败");
        } else {
            return new ResultVO<UserVo>(true, "插入成功", UserVo.changeToVo(user));
        }
    }
    private Map<String, Object> resultMap(boolean flag, String message) {
        var result = new HashMap<String, Object>();
        result.put("success", false);
        result.put("message", message);
        return result;
    }
    @GetMapping("/message")
    public String message() {
        return "用户权限访问";
    }
}
